package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.ArenaRecordPo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author James
 * @date 2022/1/22 16:39
 **/
public class ArenaRecordDAO {
    private static final String
            REPLACE_ONE = "replace into `arenarecord` (`pCode`, `attackerCode`, `battleResult`,`date`, `defenderCode`, `recordCode`,`scoreChangeAttacker`," +
            " `scoreChangeDefender`, `syncdb`, `updateTime`) values (?,?,?,?,?,?,?,?,?,?)",
            SELECT_ALL = "select * from `arenarecord` where `pCode` = ?",
            DELETE = "delete from `arenarecord` where `pCode` = ? and `updateTime` != ?";

    public static List<ArenaRecordPo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, ArenaRecordPo.builder);
    }

    public static boolean replace(List<ArenaRecordPo> arenaRecordPoList) {
        List<Object[]> list = new ArrayList<>();
        for (ArenaRecordPo arenaRecordPo : arenaRecordPoList) {
            Object[] objects = new Object[]{arenaRecordPo.getPlayerCode(),arenaRecordPo.getAttackerCode(),arenaRecordPo.getBattleResult(),arenaRecordPo.getDate(),
            arenaRecordPo.getDefenderCode(),arenaRecordPo.getRecordCode(),arenaRecordPo.getScoreChangeAttacker(),arenaRecordPo.getScoreChangeDefender(),
            arenaRecordPo.getSyncdb(),arenaRecordPo.getUpdateTime()};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == arenaRecordPoList.size();
    }

    public static void delete(String pCode, long updateTime) {
        DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});
    }
}
